package com.wythe.mall.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wythe.mall.R;
import com.wythe.mall.adapter.ImageShopAdapter;
import com.wythe.mall.base.BaseActivity;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.utils.ApiOkHttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RecommendActivity extends BaseActivity implements View.OnClickListener {
    private GridView mGridView;
    private ImageShopAdapter mShopAdapter;
    private List<ShopBean> list;
    private TextView mTopTitle;
    private ImageView mBackView;
    private int goodId = 0;
    private String mTitle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommend);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            goodId = bundle.getInt("id");
            mTitle = bundle.getString("title");
            int type = bundle.getInt("type");
            if (type == 1) {
                initData();
            } else {
                getRecommendList(goodId);
            }
        }
        initView();
    }

    public void initView() {
        mGridView = (GridView) findViewById(R.id.gv_shop);
        mBackView = (ImageView) findViewById(R.id.title_left_button);
        mTopTitle = (TextView) findViewById(R.id.titleText);
        mTopTitle.setText(mTitle);
        mBackView.setOnClickListener(this);

    }

    public void initData() {
        getShopList(goodId);
    }

    public void getRecommendList(int id) {
        JSONObject params = new JSONObject();
        try {
            params.put("bannerId", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiOkHttp.getBannerShopList(getActivity(), params, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                if (list == null) {
                    list = new ArrayList<>();
                }
                list.clear();
                try {
                    JSONArray array = data.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        ShopBean bean = new Gson().fromJson(array.getJSONObject(i).toString(), ShopBean.class);
                        list.add(bean);
                    }
                    mShopAdapter = new ImageShopAdapter(getActivity(), list);
                    mGridView.setAdapter(mShopAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });
    }

    public void getShopList(int type) {
        JSONObject params = new JSONObject();
        try {
            params.put("indexLabelId", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiOkHttp.getShopList(getActivity(), params, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                if (list == null) {
                    list = new ArrayList<>();
                }
                list.clear();
                try {
                    JSONArray array = data.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        ShopBean bean = new Gson().fromJson(array.getJSONObject(i).toString(), ShopBean.class);
                        list.add(bean);
                    }
                    mShopAdapter = new ImageShopAdapter(getActivity(), list);
                    mGridView.setAdapter(mShopAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.title_left_button:
                finish();
                break;
        }
    }
}
